package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.JwtUtils;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.media.dtos.WmTypeEnum;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.media.dtos.WmUserLoginDto;
import com.heima.model.media.dtos.WmUserTokenDto;
import com.heima.wemedia.entity.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-02-01
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    /**
     * 审核通过  创建自媒体用户
     * @param dto
     * @return
     */
    @Override
    public Integer createAndUpdateWmUser(WmUserDto dto) {
        LambdaQueryWrapper<WmUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmUser::getApUserId,dto.getApUserId());
        WmUser wmUser = this.getOne(lqw);
        //如果不存在数据  新增 否则  修改更新
        if (wmUser == null){
            WmUser user = buildWmUser(dto, null);
            this.save(user);
            return user.getId();
        }else {
            WmUser user = buildWmUser(dto, wmUser.getId());
            this.updateById(user);
            return user.getId();
        }
    }
    private final static Integer TOKEN_TIME = 120;

    @Autowired
    private BCryptPasswordEncoder encoder;
    @Override
    public ResponseResult login(WmUserLoginDto dto) {
        //条件查询
        LambdaQueryWrapper<WmUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmUser::getName,dto.getName());
        WmUser adUser = this.getOne(lqw);
        //判断adUser是否存在
        if (adUser == null){
            throw new LeadException(AppHttpCodeEnum.AD_USER_NOT_EXIST);
        }
        String passwordDB = adUser.getPassword();
        String password = dto.getPassword();
        boolean b = encoder.matches(password, passwordDB);
        if (!b){
            //如果密码不对  抛出异常
            throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //设置token
        Map<String,Object> map = new HashMap<>();
        String token = JwtUtils.generateTokenExpireInMinutes(adUser.getId(), TOKEN_TIME);
        map.put("token",token);
        AdUserDto adUserDto = new AdUserDto();
        BeanUtils.copyProperties(adUser,adUserDto);
        map.put("user",adUserDto);
        return ResponseResult.ok(map);
    }

    private WmUser buildWmUser(WmUserDto dto,Integer wmUserId) {
        WmUser user = new WmUser();
        user.setId(wmUserId);
        user.setName(dto.getName());
        user.setPassword(dto.getPassword());
        user.setStatus(dto.getStatus());
        user.setPhone(dto.getPhone());
        user.setType(WmTypeEnum.GEREN.getCode());
        if (wmUserId == null){
            user.setApUserId(dto.getApUserId());
            user.setCreatedTime(new Date());
        }
        return user;
    }
}
